package com.tosit.bmx.controller.user;

import com.tosit.bmx.common.json.JSONModel;
import com.tosit.bmx.common.json.JSONUtil;
import com.tosit.bmx.domian.*;
import com.tosit.bmx.service.user.LevelService;
import com.tosit.bmx.service.user.TemporaryService;
import com.tosit.bmx.service.user.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.List;

/**
 * 考试管理类
 *
 * @author tsl
 * @create 2018-04-27 16:22
 **/
@Controller
@RequestMapping()
public class TemporaryController {
    @Autowired
    LevelService levelService;
    @Autowired
    TemporaryService temporaryService;
    @Autowired
    UserService userService;

    /**
     * 根据用户id查找用户所能关联的所有权限（题型，科目，车型,套题，前提题型未被禁用）
     * 还有用户信息
     *
     * @param
     * @return
     * @author tsl
     * @date 2018/4/24 9:38
     */
    @RequestMapping(value = "/temprorary/level", method = RequestMethod.GET)
    public void gainLevelsAndSeByUser(HttpServletResponse response) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getSession().getAttribute("user");
        //用户能用的权限
        List<CartypeVoEnhance> uservos = levelService.gainLevelsAndSeByUser(user.getId());
        if (uservos != null) {
            JSONModel.put("uservos", uservos);
            JSONModel.put("user", user);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 删掉上次考试信息
     * 通过所选套题获取题目,还有用户信息
     * 植入新的考试信息
     *
     * @param initTest 包含  List<Integer> seids（套题ids）; Integer type(是否随机取题 1随机 2不随机);
     * @return testMsg 考试车型，科目，题型的信息
     * @author tsl
     * @date 2018/4/28 9:52
     */
    @RequestMapping(value = "/temprorary/select", method = RequestMethod.PUT)
    public void getSubjectBySeIds(@RequestBody InitTest initTest, HttpServletResponse response) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getSession().getAttribute("user");
        List<TempSubject> tempSubject = temporaryService.initTest(initTest.getSeids(), initTest.getType(), user.getId());
        TestMsg testMsg = temporaryService.getCarCourse(initTest.getSeids().get(0));
        if (tempSubject != null) {
            JSONModel.put("tempSubject", tempSubject);
            JSONModel.put("user", user);
            JSONModel.put("testMsg", testMsg);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


    /**
     * 继续做题
     *
     * @param isCurrentErrSubject 判断是否是错题重做发的请求
     * @return testMsg 考试车型，科目，题型的信息
     * @author tsl
     * @date 2018/4/28 9:52
     */
    @RequestMapping(value = "/temprorary/continue", method = RequestMethod.GET)
    public void getSubjectBySeIds(HttpServletResponse response, Integer isCurrentErrSubject) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getSession().getAttribute("user");
        List<TempSubject> tempSubject = temporaryService.contineTest(user.getId());
        if (isCurrentErrSubject !=null && isCurrentErrSubject == 1) {
            for (Iterator<TempSubject> iterator = tempSubject.iterator(); iterator.hasNext(); ) {
                TempSubject temp = iterator.next();
                temp.setChoose(0);
                temp.setFrist(0);
                temp.setStatus(0);
            }
        }
        TestMsg testMsg = temporaryService.getCarCourseq(tempSubject.get(0).getSsid());
        if (tempSubject != null) {
            JSONModel.put("tempSubject", tempSubject);
            JSONModel.put("user", user);
            JSONModel.put("testMsg", testMsg);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 继续做错题（先删除对的题，再获取临时题） ，当前考试的错题
     *
     * @param torders 也就是temporary.torder字段的集合
     * @return testMsg 考试车型，科目，题型的信息
     * @author tsl
     * @date 2018/4/28 9:52
     */
    @RequestMapping(value = "/temprorary/test/continue", method = RequestMethod.PUT)
    public void getDSubjectBySeIds(@RequestBody List<Integer> torders, HttpServletResponse response) {
        if (torders == null || torders.size() == 0) {
            JSONModel.put("status", "fail");
        } else {
            Subject subject = SecurityUtils.getSubject();
            User user = (User) subject.getSession().getAttribute("user");
            temporaryService.deleteTem(torders, user.getId());

            List<TempSubject> tempSubject = temporaryService.contineTest(user.getId());
            TestMsg testMsg = temporaryService.getCarCourseq(tempSubject.get(0).getSsid());
            if (tempSubject != null) {
                JSONModel.put("tempSubject", tempSubject);
                JSONModel.put("user", user);
                JSONModel.put("testMsg", testMsg);
            } else {
                JSONModel.put("status", "fail");
            }
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 退出考试（删除用户的临时题）
     *
     * @author tsl
     * @date 2018/4/28 9:52
     */
    @RequestMapping(value = "/temprorary/test/exit", method = RequestMethod.GET)
    public void getDSubjectBytors(HttpServletResponse response) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getSession().getAttribute("user");
        Integer result = temporaryService.deleteByUserId(user.getId());
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


    /**
     * 判断是否有考题
     *
     * @author tsl
     * @date 2018/4/28 9:52
     */
    @RequestMapping(value = "/temprorary/isExsit", method = RequestMethod.GET)
    public void isExsit(HttpServletResponse response) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getSession().getAttribute("user");
        Temporary tempSubject = temporaryService.isExsit(user.getId());
        if (tempSubject != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


    /**
     * 修改考试题
     *
     * @param temporary 不用temprorary的id，这里用排序和用户id（uid）修改题状态,排序也就是temporary.torder字段
     * @author tsl
     * @date 2018/4/27 15:58
     */
    @RequestMapping(value = "/temprorary", method = RequestMethod.PUT)
    public void modifyTemp(Temporary temporary, HttpServletResponse response) {
        Integer result = temporaryService.modifyTem(temporary);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


    /**
     * 模拟考试
     *
     * @param courId 科目id
     * @param number 临时身份证
     * @return
     * @author tsl
     * @date 2018/4/30 12:49
     */
    @RequestMapping(value = "/temprorary/moni", method = RequestMethod.GET)
    public void getTestSubject(Integer courId, String number, HttpServletResponse response) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getSession().getAttribute("user");
        user.setTempcard(number);
        userService.modifyUser(user);
        List<TempSubject> tempSubject = temporaryService.getRadomSub(courId, user.getId());
        TestMsg testMsg = temporaryService.getCarCourseq(tempSubject.get(0).getSsid());
        if (tempSubject != null) {
            JSONModel.put("tempSubject", tempSubject);
            JSONModel.put("user", user);
            JSONModel.put("testMsg", testMsg);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 跳转到模拟考试界面
     *
     * @param type     1汉 2藏
     * @param courseId 科目id
     * @param status   模拟考试 1
     * @return
     * @author tsl
     * @date 2018/4/28 9:52
     */
    @RequestMapping(value = "/test/{type}-{status}-{number}-{courseId}.html", method = RequestMethod.GET)
    public String toTest(Model model, @PathVariable("status") Integer status, @PathVariable("type") Integer type, @PathVariable("courseId") String courseId, @PathVariable("number") String number) {
        model.addAttribute("type", status);
        model.addAttribute("courseId", courseId);
        model.addAttribute("number", number);
        String url = "/user/testpage/test_moni";
        if (type == 2) {
            url = "/user/testpage/ztest_moni";
        }
        return url;
    }


    /**
     * 跳转到考试界面
     *
     * @param type   1汉 2藏
     * @param seids  套题id eg：123\545\123 跳转过去后需要你自己解析，并靠它去获取数据
     * @param radom  1随机 2不随机
     * @param status 选题考试 2
     * @return
     * @author tsl
     * @date 2018/4/28 9:52
     */
    @RequestMapping(value = "/toTemprorary/{type}-{status}-{seids}-{radom}.html", method = RequestMethod.GET)
    public String toTemprorary(Model model, @PathVariable("status") Integer status, @PathVariable("type") Integer type, @PathVariable("seids") String seids, @PathVariable("radom") String radom) {
        model.addAttribute("type", status);
        model.addAttribute("seids", seids);
        model.addAttribute("radom", radom);
        String url = "/user/testpage/test_normal";
        if (type == 2) {
            url = "/user/testpage/ztest_normal";
        }
        return url;
    }

    /**
     * 跳转到 错题考试界面或者是 继续做题考试界面
     *
     * @param type   1汉 2藏
     * @param status 错题考试 3 继续考试 4
     * @return
     * @author tsl
     * @date 2018/4/28 9:52
     */
    @RequestMapping(value = "/toTemprorary/{type}-{status}-test.html", method = RequestMethod.GET)
    public String toErrorTemprorary(Model model, @PathVariable("status") Integer status, @PathVariable("type") Integer type) {
        model.addAttribute("type", status);
        String url = null;
        if (type == 1) {
            if (status == 3) {
                url = "/user/testpage/test_err";//我的错题
            } else if (status == 4) {
                url = "/user/testpage/test_continue";//继续考试
            } else if (status == 5) {
                url = "/user/testpage/test_current_err";//错题重做
            }
        } else if (type == 2) {
            if (status == 3) {
                url = "/user/testpage/ztest_err";
            } else if (status == 4) {
                url = "/user/testpage/ztest_continue";
            } else if (status == 5) {
                url = "/user/testpage/ztest_current_err";//错题重做
            }
        }
        return url;
    }


}
